//
//  StackTestVC.swift
//  CooperSwift_Example
//
//  Created by 计海峰 on 2021/5/13.
//  Copyright © 2021 CocoaPods. All rights reserved.
//

import UIKit
import RxSwift

class StackTestVC: UIViewController {
    
    let disposeBag = DisposeBag()
    
    override func viewDidLoad() {
        super.viewDidLoad()

        view.backgroundColor = UIColor.white
        layoutStackView()
        // Do any additional setup after loading the view.
    }
    
    
      func layoutStackView() {
        
        let stackView = stackViewWithAxis(axis: .horizontal, alignment: .center, distribution: .fillProportionally)
        stackView.backgroundColor = .random()
          view.addSubview(stackView)
          stackView.snp.makeConstraints { (make) in
              make.center.equalTo(self.view)
              make.left.equalTo(0)
              make.right.equalTo(0)
              make.height.equalTo(60)
          }
        
        let leftStackView = stackViewWithAxis(axis: .horizontal, alignment: .center, distribution: .fill)
          for i in 0..<2 {
              let label = createLabel()
              label.text = "\("哈哈")" + "\(10 + i)"
              leftStackView.addArrangedSubview(label)
              label.snp.makeConstraints {
                $0.top.bottom.equalTo(leftStackView)
                $0.width.equalTo(60)
              }
          }
        
        stackView.addArrangedSubview(leftStackView)
//        leftStackView.snp.makeConstraints {
//            $0.width.equalTo(120)
//        }
        
//        let tf = UITextField.init().then {
//            $0.backgroundColor = .red
//        }
//        stackView.addArrangedSubview(tf);
//        tf.snp.makeConstraints {
//            $0.top.bottom.equalTo(stackView)
//        }
        
        let lb = UILabel.init().then {
            $0.backgroundColor = .red
        }
        stackView.addArrangedSubview(lb);
        lb.snp.makeConstraints {
            $0.top.bottom.equalTo(stackView)
        }
        lb.text = "中间label"
        
        let rightStackView = stackViewWithAxis(axis: .horizontal, alignment: .center, distribution: .fill)
          for i in 0..<1 {
              let label = createLabel()
              label.text = "\("哈哈")" + "\(20 + i)"
            rightStackView.addArrangedSubview(label)
          }
        stackView.addArrangedSubview(rightStackView)
        rightStackView.snp.makeConstraints {
            $0.width.equalTo(60)
        }
        
      }
      
      func stackViewWithAxis(axis: NSLayoutConstraint.Axis, alignment: UIStackView.Alignment, distribution: UIStackView.Distribution) -> UIStackView {
          let stackView = UIStackView()
          // 水平布局
          stackView.axis = axis
          // 对齐方式
          stackView.alignment = alignment
          //
          stackView.distribution = distribution
          stackView.spacing = 8
          return stackView
      }
      
      func createLabel() -> UILabel {
          let label = UILabel()
          label.textColor = UIColor.black
          label.numberOfLines = 0
          label.backgroundColor = UIColor.init(red: CGFloat(arc4random_uniform(256))/255.0, green: CGFloat(arc4random_uniform(256))/255.0, blue: CGFloat(arc4random_uniform(256))/255.0, alpha: 1.0)
          return label
      }

}
